;
(function () {
  new Vue({
    el: '#contract-list',
    data: {
      data: {}, // 合同列表数据
      keyword: '',
      beginIntentAmount: null,
      endIntentAmount: null,
      listMenu: [
        {
          'name': '草稿',
          'code': 'CG',
          'num': 0
        },
        {
          'name': '已作废',
          'code': 'GC',
          'num': 0
        },
        {
          'name': '审批中',
          'code': 'WA',
          'num': 0
        },
        {
          'name': '审批拒绝',
          'code': 'RJ',
          'num': 0
        },
        {
          'name': '审批通过',
          'code': 'CF',
          'num': 0
        },
        {
          'name': '已支付',
          'code': 'PD',
          'num': 0
        },
        {
          'name': '已转合同',
          'code': 'TC',
          'num': 0
        },
        {
          'name': '待退款',
          'code': 'WT',
          'num': 0
        },
        {
          'name': '已退款',
          'code': 'YT',
          'num': 0
        },
      ],
      selectedListMenuCode: [],
      currentPage: 1,
      totalPage: 0,
      totalCount: 0,
      limit: ['10'],
      isShowSubmitLayer: false,
      submitLayerData: {
        companyName: '',
        beginDate: '',
        endDate: '',
        id: ''
      },
      columns: '#', // 排序的列
      orders: 'DESC', // 排序方式
      stOrder: '1', // 0 没有排序  1 生序   2 降序
      tbTitleWidth: { width: 0 + 'px' },
      tbTotalWidth: { width: 957 + 'px' },
      tbOperateWidth: 123,
      tbTitle: [],
      wzTableTitle: [],
      jdTableTitle: [],
      pzTableTitle: [],
      sortTableTitle: [],
      hasCustomConfig: 'N',
      pageCounts: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      configType: 'JD',
      isShowTableType: false,
      isShowTableLayer: false,
      curModule: 'ZZHT',
      tableType: 'scroll',   //  scroll滑动表  fixed固定表
      isScroll: false,
      state: null,
      stateflag: false,
      refundflag: false,
      errData: {},
      refundMome: '',
      yxId: null,
      refund: { name: '', btime: '', etime: '', amount: '', ltime: '' },
      rwidth: 0,
      isExtenseFlag: false,
      quanxian: JSON.parse(localStorage.getItem('permissions')),
      butList:[{
        name:'详情',
      },{
        name:'编辑',
        permissions:'c156',
      },{
        name:'作废',
        permissions:'c158',
      },{
        name:'确认意向',
        permissions:'c157',
      },{
        name:'退款',
        permissions:'c160',
      }],

      titleData: [
        { name: "序", value: "index", width: "50" },
        { name: "客户名称", value: "customerName" },
        { name: "意向项目", value: "projectName"},
        { name: "意向周期", value: "cycle", width: "180" },
        { name: "确认时间", value: "confirmTimeText", width: "180", isRunk: true, column: 'CFT', orders: ['ASC','DESC'], order:''},
        { name: "意向金(元)", value: "intentAmount"},
        { name: "最晚付款时间", value: "intentPaymentDeadlineText",width:'150'},
      ],

      beginPaymentDeadline:'',
      endPaymentDeadline:'',
      beginConfirmTime:'',
      endConfirmTime:'',
			refunddate: ''
    },
    filters: {
      formatDate: function (time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      formatDatehms: function (time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd HH:mm:ss')
      },
      formatUndefined: function (item) {
        if (!item) return '--'
        return item
      },
      formatNum: function (item) {
        if (item === undefined) return '--'
        return hlNumberUtil.formatNum(item)
      }
    },
    created() {
    },
    methods: {
      selectMenuList(){
        this.currentPage = 1
        this.search()
      },
      // 获取组件表头点击反馈信息
      onClickRunk(val){
        this.columns = val.column
        this.orders = val.order
        this.search()
      },
      resetTabbar() {
        this.selectedListMenuCode = []
        this.currentPage = 1
        this.search()
      },
      openSearch: function () {
        if (this.isExtenseFlag == true) {
          this.isExtenseFlag = false;
          return false;
        }
        if (this.isExtenseFlag == false) {
          this.isExtenseFlag = true;
          return false;
        }
      },
      // 查询合同
      search: function () {
        var self = this;
        var data = {
          keyword: self.keyword.trim() || '#',
          begin_intent_amount: self.beginIntentAmount || '#',
          end_intent_amount: self.endIntentAmount || '#',
          begin_payment_deadline: self.beginPaymentDeadline ? self.beginPaymentDeadline + ' 00:00:00' : '#',
          end_payment_deadline: self.endPaymentDeadline ? self.endPaymentDeadline + ' 23:59:59' : '#',
          begin_confirm_time: self.beginConfirmTime ? self.beginConfirmTime + ' 00:00:00' : '#',
          end_confirm_time: self.endConfirmTime ? self.endConfirmTime + ' 23:59:59' : '#',
          audit_state: self.selectedListMenuCode.length ? self.selectedListMenuCode.join() : '#',
          column: 'CFT',
          order: self.orders,
          page: self.currentPage,
          limit: self.limit[0],
        }

        hlhttp.get({
          url: 'contract/intent/_search',
          data: data,
          success: function (data) {
            data.intents.map(item => {
              item.cycle = hlDateUtil.formatUTCDate(item.intentStartDate, 'yyyy-MM-dd') + '至' + hlDateUtil.formatUTCDate(item.intentEndDate, 'yyyy-MM-dd')
              item.confirmTimeText = hlDateUtil.formatUTCDate(item.confirmTime, 'yyyy-MM-dd HH:mm:ss')
              item.intentPaymentDeadlineText = hlDateUtil.formatUTCDate(item.intentPaymentDeadline, 'yyyy-MM-dd HH:mm:ss')
            })

            self.totalCount = data.totalCount
            self.totalPage = data.totalPage
            self.data = data;
            self.listMenu.map(item => {
              if (item.code == 'CG') {
                item.num = data.cGCount
              }
              if (item.code == 'GC') {
                item.num = data.gCCount
              }
              if (item.code == 'WA') {
                item.num = data.wACount
              }
              if (item.code == 'RJ') {
                item.num = data.rJCount
              }
              if (item.code == 'CF') {
                item.num = data.cFCount
              }
              if (item.code == 'PD') {
                item.num = data.pDCount
              }
              if (item.code == 'TC') {
                item.num = data.tCCount
              }
              if (item.code == 'WT') {
                item.num = data.wTCount
              }
              if (item.code == 'YT') {
                item.num = data.yTCount
              }
            })

          }
        })
      },
      // 重置
      reset: function () {
        this.keyword = ''
        this.beginIntentAmount = null
        this.endIntentAmount = null
        this.beginPaymentDeadline = ''
        this.endPaymentDeadline = ''
        this.beginConfirmTime = ''
        this.endConfirmTime = ''
        this.currentPage = 1
        this.search()
      },
      setorders() {
        this.orders = this.orders == 'ASC' ? 'DESC' : 'ASC';
        this.search()
      },
      createyx() {
        hlUrlUtil.goTo('yxCreate', { ddtab: true, clear: true }, true)
      },
      setMoreNum(arr) {
        this.limit = [arr[0].code]
        this.currentPage = 1
        this.search()
      },
      ifYxBook(id, item) {
        this.refund = { name: item.customerName, btime: item.intentStartDate, etime: item.intentEndDate, amount: item.intentAmount, ltime: item.intentPaymentDeadline }
        this.yxId = id;
        hlhttp.put({
          url: 'contract/intent/check_confirm',
          data: { id: this.yxId },
          success: () => {
            this.state = 0;
            this.stateflag = true;
          },
          error: (err) => {
            let res = JSON.parse(err.responseText);
            this.stateflag = true;
            if (res.code == 'INFO_WRONG') {
              this.state = 1;
            }
            if (res.code == 'UNIT_RENTED') {
              this.state = 2;
              this.errData = JSON.parse(res.msg);
            }
          }
        })
      },
      queryYx() {
        if (this.state > 0) {
          this.stateflag = false;
          return
        }
        hlhttp.put({
          url: 'contract/intent/_confirm',
          data: { id: this.yxId },
          success: () => {
            this.stateflag = false;
            this.search()
          }
        })
      },
      hideYx() {
        this.stateflag = false;
      },
      cancelYx(id) {
        hllayer.confirm1('作废意向书时会同步作废意向金账单<br/>此操作不可能逆，确认作废意向书吗？', () => {
          hlhttp.put({
            url: 'contract/intent/_cancel',
            data: { id: id },
            success: () => {
              hl.toast('操作成功')
              this.search()
            }
          })
        })
      },
      //显示退款框框
      refundShow(item) {
        this.yxId = item.id;
        this.refund = { name: item.customerName, btime: item.intentStartDate, etime: item.intentEndDate, amount: item.intentAmount, ltime: item.intentPaymentDeadline }
        this.refundflag = true;
      },
      //关闭退款框框
      refundHide() {
        this.refundflag = false;
        this.refundMome = '';
        this.refunddate = ''
      },
      //退款
      refundYx() {
        if (!this.refunddate) {
          hl.toast('请选择退款时间')
          return
        }
        hlhttp.put({
          url: 'contract/intent/_refund',
          data: { id: this.yxId, refund_reason: this.refundMome, refund_time: this.refunddate },
          success: () => {
            hl.toast('操作成功')
            this.search()
            this.refundflag = false;
          }
        })
      },
      showScroll() {
        this.isScroll = true;
      },
      hideScroll() {
        this.isScroll = false;
      }
    },
    mounted: function () {
      var self = this;
      hlSelectUi(true)
      this.search()
      this.butList.map((item,index) =>{
        if(!this.quanxian.includes(item.permissions)){
          this.butList.splice(index,1)
        }
      })
    }
  })

}())
